<?php

$container_bl=join('&nbsp;', $form->_button_container['BL']);
$container_br=join('&nbsp;', $form->_button_container['BR']);
$container_tr=join('&nbsp;', $form->_button_container['TR']);

if ($form->_status=='delete' || $form->_action=='delete' || $form->_status=='unknow_record'):
	echo $form->output;
else:
$transac=$form->transac->value;
$usuario=$this->datasis->dameval("SELECT usuario FROM gser WHERE transac='$transac'");

$campos=$form->template_details('itcruce');
$scampos  ='<tr id="tr_itcruce_<#i#>">';
$scampos .='<td class="littletablerow" align="left" >'.$campos['cuenta']['field'].$campos['create']['field'].'</td>';
$scampos .='<td class="littletablerow" align="left" >'.$campos['desca']['field'].'</td>';
$scampos .='<td class="littletablerow" align="left" >'.$campos['ruc']['field'].'</td>';
$scampos .='<td class="littletablerow" align="left" >'.$campos['tipod']['field'].' '.$campos['seried']['field'].' - '.$campos['numerod']['field'].'</td>';
$scampos .='<td class="littletablerow" align="left" >'.$campos['tipo']['field'].'</td>';
$scampos .='<td class="littletablerow" align="left" >'.$campos['monedaori']['field'].'</td>';
$scampos .='<td class="littletablerow" align="right">'.$campos['importe']['field'].'</td>';
$scampos .='<td class="littletablerow" align="right">'.$campos['importemn']['field'].'</td>';
$scampos .='<td class="littletablerow" align="right">'.$campos['creditos']['field'];

$scampos .= '<td class="littletablerow"><a href=# onclick="del_itcruce(<#i#>);return false;">'.img("images/delete.jpg").'</a></td></tr>';
$campos=$form->js_escape($scampos);

if(isset($form->error_string)) echo '<div class="alert">'.$form->error_string.'</div>';

echo $form_scripts;
echo $form_begin;

$link=base_url()."common/numcruce/";
$link3=base_url()."common/cfecha";
$link4=base_url()."common/liqui";
if($form->_status!='show'){ ?>

<script language="javascript" type="text/javascript">
var itcruce_cont=<?php echo $form->max_rel_count['itcruce']; ?>;

$(function(){
	$(".inputnum").numeric(".");
	totalizar();
});
function numcruce(){
	
	$.ajax({
		url: "<?=$link?>",
		success: function(msg){
			if(msg){
				$("#numeroi").val(msg);
			} else {
				alert("No es posible generar un numero");
			}
		}
	});
}
function cfecha(){
	
	fecha    = $("#fecha").val();

	$.ajax({
		url: "<?=$link3?>"+'/'+fecha,
		success: function(msg){
			if(msg){
				alert("Valor de la moneda "+msg+" de FECHA "+fecha);
				$("#cambio").val(msg);
				liqui(msg);
			} else {
				alert("Registrar el valor de la moneda para la FECHA "+fecha);
				$("#cambio").val(0);
			}
		}
	});

}
function liqui(cambio){
	
	$.ajax({
		url: "<?=$link4?>"+'/'+cambio,
		success: function(msg){
			if(msg){
				//alert("CXP  generadas al tipo de cambio "+cambio);
			} else {
				alert("CXP no generadas al tipo de cambio "+cambio);
			}
		}
	});

}
function importe(id){
	
	var ind     = id.toString();
	var vexento = $("#vexento_"+ind).val();
	var vcreditos = $("#tipo_"+ind).val();
	
	var cambio =$("#cambio").val();
		
	var importe2= Number($("#importe_"+ind).val());
	
	var importemn=importe2*cambio;
	
	$("#importemn_"+ind).val(importemn);
	//$("#creditos_"+ind).attr('disabled','disabled');
	//$("#exento_"+ind).attr('disabled','disabled');
	
	if(vcreditos=='D'){

		if(vexento=='S'){
			$("#exento_"+ind).val(importe2);	

		}else{
			$("#exento_"+ind).val(0);
		}
		
		$("#creditos_"+ind).val(0);	
	
	}else{
	
		$("#creditos_"+ind).val(importe2);
	
	}
	
	totalizar();
}
function totalizar(){
	
	var totalg =0;
	var totals =0;
	var texento =0;
	var tcreditos =0;
	var ivap   =18;
	var importe=0;
	var rete=0;
	
	var arr=$('input[name^="importe_"]');
	jQuery.each(arr, function() {
		nom=this.name;
		pos=this.name.lastIndexOf('_');
		if(pos>0){
			importe = Number(this.value);	
			totals  = (totals+importe);
		}
	});
	
	
	var arr=$('input[name^="creditos_"]');
	jQuery.each(arr, function() {
		nom=this.name;
		pos=this.name.lastIndexOf('_');
		if(pos>0){
			creditos  = Number(this.value);	
			tcreditos = (tcreditos+creditos);
		}
	});
	var cambio =$("#cambio").val();
	
	
	$("#tcreditosd").val(roundNumber(tcreditos,2));
	$("#tdebitosd").val(roundNumber(totals-tcreditos,2));
	
	var tcreditosd =$("#tcreditosd").val();
	var tdebitosd =$("#tdebitosd").val();
	
	totds=roundNumber(tcreditosd*cambio,2);
	totcs=roundNumber(tdebitosd*cambio,2);
	
	$("#tcreditoss").val(roundNumber(totds,2));
	$("#tdebitoss").val(roundNumber(totcs,2));
	
	var tcreditoss =$("#tcreditoss").val();
	var tdebitoss =$("#tdebitoss").val();
	
	$("#difed").val(tdebitosd-tcreditosd);
	$("#difes").val(tdebitoss-tcreditoss);
		
	//subtotal=totals-tcreditos-tcreditos;
	//$("#totalg").val(roundNumber((subtotal),2));

}
function add_itcruce(){
	var htm = <?php echo $campos; ?>;
	can = itcruce_cont.toString();
	con = (itcruce_cont+1).toString();
	htm = htm.replace(/<#i#>/g,can);
	htm = htm.replace(/<#o#>/g,con);
	$("#__INPL__").after(htm);
	$("#cana_"+can).numeric(".");
	$('#cuenta_'+can).focus();
	itcruce_cont=itcruce_cont+1;
}
function del_itcruce(id){
	id = id.toString();
	$('#tr_itcruce_'+id).remove();
	totalizar();
}
</script>
<?php } ?>
<table align='center' width="90%" style='background: #EEEEEE'>
	<tr>
		<?php if ($form->_status=='show') { ?>
		<td>
		<a href="#" onClick="window.open('<?php echo base_url() ?>formatos/verhtml/LIQUI/<?php echo $form->transac->value ?>', '_blank', 'width=800, height=600, scrollbars=Yes, status=Yes, resizable=Yes, screenx='+((screen.availWidth/2)-400)+',screeny='+((screen.availHeight/2)-300)+'');" heigth="600" >
		<img src='<?php echo base_url() ?>images/html_icon.gif'></a>
		<a href="#" onClick="window.open('<?php echo base_url() ?>formatos/ver/LIQUI/<?php echo $form->transac->value ?>', '_blank', 'width=800, height=600, scrollbars=Yes, status=Yes, resizable=Yes, screenx='+((screen.availWidth/2)-400)+',screeny='+((screen.availHeight/2)-300)+'');" heigth="600" >
		<img src='<?php echo base_url() ?>images/pdf_logo.gif'></a> Comprobante 
		</td>
	<?php } ?>
	  <td colspan="3" align="right"><?php echo $container_tr?><?php echo $container_br?></td>
  </tr>
</table>
<table align='center'  width="95%">
	<tr>
		<td>
         <table align='center' width="100%">
	<tr>
		<td>
		<table width='100%'><tr><td>
		<fieldset style='border: 2px outset #9AC8DA;background: #FFFFFF;'>
		<legend class="titulofieldset" style='color: #2E4B77;'>Informaci&ograve;n de la Liquidaci&oacute;n</legend>
			<table style="margin: 0; width: 100%;">
			<tr>
				<td class="littletableheader"><?php echo $form->seriei->label;    ?></td>
				<td class="littletablerow" colspan=2 ><?php echo $form->comp->output;   ?> <?php echo $form->seriei->output;   ?>-<?php echo $form->numeroi->output;   ?></td>
			</tr>
			<tr>
				<td class="littletableheader"><?php echo $form->seriep->label;    ?></td>
				<td class="littletablerow" colspan=2 ><?php echo $form->seriep->output;   ?>-<?php echo $form->numero->output;?></td>
			</tr>
			<tr>
			  <td class="littletableheader"><?php echo $form->orden->label;    ?></td>
			  <td colspan="2" class="littletablerow"><?php echo $form->orden->output;   ?></td>
			  </tr>
			<tr>
				<td class="littletableheader"><?php echo $form->fecha->label;    ?></td>
				<td colspan="2" class="littletablerow">   <?php echo $form->fecha->output;   ?></td>
			</tr>
			<tr>
				<td class="littletableheader"><?php echo $form->ccosto->label;    ?></td>
				<td colspan="2" class="littletablerow">   <?php echo $form->ccosto->output;   ?></td>
			</tr>
			<tr>
			  <td class="littletableheader"><?php echo $form->moneda->label     ?></td>
			  <td colspan="2" class="littletablerow"><?php echo $form->moneda->output    ?></td>
			</tr>
			<tr>
			  <td class="littletableheader"><?php echo $form->cambio->label     ?></td>
			  <td colspan="2" class="littletablerow"><?php echo $form->cambio->output    ?></td>
			</tr>
			</table>
			</fieldset>
		</td><td width="45%">
			<fieldset style='border: 2px outset #9AC8DA;background: #FFFFFF;'>
			<legend  class="titulofieldset" style='color: #2E4B77;'>Identificaci&oacute;n del Proveedor </legend>
			<table  style="margin: 0; width: 100%;">
			<tr>
				<td width="33%" class="littletableheader"><?php echo $form->proveed->label;  ?></td>
				<td class="littletablerow"><?php echo $form->proveed->output; ?></td>
				</tr>
			<tr>
				<td class="littletableheader"><?php echo $form->nombre->label;  ?></td>
				<td class="littletablerow"><?php echo $form->nombre->output;  ?></span></td>
			</tr>
			<tr>
			  <td class="littletableheader"><?php echo $form->rif->label; ?></td>
			  <td class="littletablerow"><?php echo $form->rif->output;   ?></td>
			</tr>
			</table>
			</fieldset>
		</td><tr></table>
		<br>
		No sobrescribir la fila, si desea modificar la liquidaci&ograve;n elimine el items y vuelvalo a crear </td>
	</tr>
	<tr>
		<td>
		<div style='overflow:auto;border: 1px solid #9AC8DA;background: #FAFAFA;height:200px'>
		<table width='95%' border='0' align="center">
			<tr id='__INPL__' class="littletablerow">
				<td bgcolor='#527D9E'>Cuenta</td>
				<td bgcolor='#527D9E'>Descripci&oacute;n</td>
				<td bgcolor='#527D9E'>Proveedor</td>
				<td bgcolor='#527D9E'>Documento</td>
				<td bgcolor='#527D9E'>Tipo</td>
				<td bgcolor='#527D9E'>Moneda</td>
				<td bgcolor='#527D9E'>Monto $ </td>
				<td bgcolor='#527D9E'>Monto S/. </td>
				<?php if ($form->_status!='show') { ?>
				<td bgcolor='#527D9E'>Creditos</td>
				<?}?>
				<?php if($form->_status!='show') {?>
					<td class="littletableheader">&nbsp;</td>
				<?php } ?>
			</tr>

			<?php $debitosd=0; $creditosd=0;  $debitoss=0; for($i=0;$i<$form->max_rel_count['itcruce'];$i++) {				
				$it_cuenta  = "cuenta_$i";
				$it_desca   = "desca_$i";
				$it_ruc     = "ruc_$i";
				$it_tipo    = "tipo_$i";
				$it_importe = "importe_$i";
				$it_importemn = "importemn_$i";
				$it_creditos= "creditos_$i";
				$it_tipod   = "tipod_$i";
				$it_seried  = "seried_$i";
				$it_numerod = "numerod_$i";
				$it_monedaori = "monedaori_$i";
				$it_create   = "create_$i";
				
				$cambio=$form->cambio->value;
				
				$debitosd  +=$form->$it_importe->value;
				$creditosd +=$form->$it_creditos->value;
				
				$debitoss  +=$form->$it_importemn->value;
				
				if($form->_status=='show'){
					$creditoss =$this->datasis->dameval("SELECT SUM(importemn) FROM itcruce WHERE transac='$transac' and tipo='C'");
					$tdebitos  =$debitoss-$creditoss;
					$tcreditos =$creditoss;
				}else{
					
					$tdebitos  =0;
					$tcreditos =0;
				}
				$tdebitod  =$debitosd-$creditosd;
				$tcreditod =$creditosd;
				
				$difes=ROUND($tdebitos-$tcreditos,2);
				$difed=ROUND($tdebitod-$tcreditod,2);
				
				//echo $form->$it_importe->value;

			?>
            <input type="hidden" id="credito" name="credito" />
			<tr id='tr_itcruce_<?php echo $i; ?>'>
				<td class="littletablerow" align="left"><?php echo $form->$it_cuenta->output; ?><?php echo $form->$it_create->output; ?></td>
				<td class="littletablerow" align="left"><?php echo $form->$it_desca->output;  ?></td>
				<td class="littletablerow" align="left"><?php echo $form->$it_ruc->output;  ?></td>
				<td class="littletablerow" align="left"><?php echo $form->$it_tipod->output;  ?><?php echo $form->$it_seried->output;  ?>-<?php echo $form->$it_numerod->output;  ?></td>
				<td class="littletablerow" align="center"><?php echo $form->$it_tipo->output;  ?></td>
				<td class="littletablerow" align="center"><?php echo $form->$it_monedaori->output;  ?></td>
				<td align="right" class="littletablerow"><?php echo $form->$it_importe->output;?></td>
				<td align="right" class="littletablerow"><?php echo $form->$it_importemn->output;?></td>
				<?php if ($form->_status!='show') { ?>
				<td class="littletablerow" align="left"><?php echo $form->$it_creditos->output;?></td>
				<?php } ?>
				<?php if($form->_status!='show') {?>
				<td class="littletablerow">
					<a href='#' onclick='del_itcruce(<?=$i ?>);return false;'><?php echo img("images/delete.jpg"); ?></a>				</td>
				<?php } ?>
			</tr>
			<?php } ?>

			<tr id='__UTPL__'>
				<td id='cueca'></td>
			</tr>
		</table>
		</div>
		<?php echo $container_bl ?>
		</td>
	</tr>
	<tr>
		<td>
		<fieldset style='border: 2px outset #9AC8DA;background: #EFEFEF;'>
		<legend  class="titulofieldset" style='color: #2E4B77;' align="right">-- Totales --</legend>
		<table width='100%'>
			<tr>
				<td width='15%'class="littletableheader" ><?php echo $form->condi1->label;    ?></td>
				<td width='30%'class="littletablerow"    ><?php echo $form->condi1->output;   ?></td>
				<td width='12%' align='right' class="littletableheader">Total D&eacute;bitos</td>
				<td width='2%' align='right' class="littletableheader">S/.</td>
				<td width='7%' class="littletablerow" align='right' ><input type="text"    id="tdebitoss" value="<?= $tdebitos?>" name="tdebitoss"   size="10"  class="input" /></td>
				<td width='8%' align='right' class="littletableheader">Cr&eacute;ditos</td>
				<td width='2%' align='right' class="littletableheader">S/.</td>
				<td width='7%' class="littletablerow" align='right' ><input type="text"    id="tcreditoss" value="<?= $tcreditos?>" name="tcreditoss"   size="10"  class="input" /></td>
				<td width='8%' align='right' class="littletableheader">Diferencia</td>
				<td width='2%' align='right' class="littletableheader">S/.</td>
				<td width='7%' class="littletablerow" align='right' ><input type="text"    id="difes" value="<?= $difes?>" name="difes"   size="10"  class="input" /></td>
			</tr>
			<tr>
				<td class="littletableheader" ></td>
				<td class="littletablerow"    ></td>
				<td align='right' class="littletableheader">Total D&eacute;bitos</td>
				<td align='right' class="littletableheader">US$</td>
				<td class="littletablerow" align='right' ><input type="text"    id="tdebitosd" value="<?= $tdebitod?>" name="tdebitosd"   size="10"  class="input" /></td>
				<td align='right' class="littletableheader">Cr&eacute;ditos</td>
				<td align='right' class="littletableheader">US$</td>
				<td class="littletablerow" align='right' ><input type="text"    id="tcreditosd" value="<?= $tcreditod?>" name="tcreditosd"   size="10"  class="input" /></td>
				<td align='right' class="littletableheader">Diferencia</td>
				<td align='right' class="littletableheader">US$</td>
				<td class="littletablerow" align='right' ><input type="text"    id="difed" value="<?= $difed?>" name="difed"   size="10"  class="input" /></td>
			</tr>
		</table>
		</fieldset>
		</td>
	</tr>
</table>

<?php echo $form_end?>
<?php echo $container_br ?>
	  <td>
	<tr>
<table>
<?php endif; ?>
